package com.tj.api.rest.account;

import com.fqgj.common.api.annotations.ParamsValidate;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.tj.api.annotations.VisitorAccessible;
import com.tj.api.rest.account.requset.AccountGrantsRequest;
import com.tj.api.rest.account.requset.BalanceRequest;
import com.tj.api.rest.account.requset.QueryBankNameRequest;
import com.tj.api.rest.account.response.AccountBalanceResponse;
import com.tj.api.rest.account.response.BalanceResponse;
import com.tj.api.rest.account.response.GrantsListResponse;
import com.tj.api.rest.account.response.QueryBankNameResponse;
import com.tj.bo.account.client.AccountQueryService;
import com.tj.bo.account.client.WithdrawService;
import com.tj.bo.account.client.enums.WithDrawStatusEnum;
import com.tj.bo.account.client.vo.AccountGrantsList;
import com.tj.bo.account.client.vo.AccountVO;
import com.tj.bo.account.client.vo.BalanceVO;
import com.tj.bo.account.client.vo.WithdrawRecordVO;
import com.tj.bo.common.conf.api.ApiResponse;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.template.config.vo.RequestLocalInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: anonymous
 * Date: 2020/06/11
 * Time: 下午2:16
 */
@RestController
public class AccountController {
    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    private AccountQueryService accountQueryService;
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private RedisConfigUtil redisDbConfigUtil;

    /**
     * 余额页面
     *
     * @return
     */
    @RequestMapping("/balance")
    public ApiResponse<BalanceResponse> getBalance() {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        Integer versionCode = RequestLocalInfo.getRequestBasicInfo().getVersionCode();
        AccountVO accountVO = accountQueryService.getAccountInfo(userCode);
        String balance = "PKR" + accountVO.getBalance();
        String grants = "PKR" + accountVO.getGrants();
        WithdrawRecordVO withdrawRecordVO = withdrawService.queryWithDrawProgress(userCode);
        //是否展示最新一笔提现中(或提现失败)页面
        boolean withdrawProcessing = withdrawRecordVO != null && withdrawRecordVO.getStatusCode() != WithDrawStatusEnum.PAYOUT_SUCCESS.getCode();
//        boolean enableRecharge = redisDbConfigUtil.getOpenRecharge() && versionCode >= redisDbConfigUtil.getRechargeVersioncode();
//        boolean enableWithdrawal = redisDbConfigUtil.getOpenWithdrawal() && versionCode >= redisDbConfigUtil.getRechargeVersioncode();
        return new ApiResponse<>(new BalanceResponse(balance, grants, withdrawProcessing, true, true));
    }

    /**
     * 虚拟账户余额和真实账户余额
     *
     * @param request
     * @return
     */
    @VisitorAccessible
    @ParamsValidate
    @RequestMapping("/account/balance")
    public ApiResponse<AccountBalanceResponse> getAccountBalance(@RequestBody BalanceRequest request) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        BalanceVO balanceInfo = accountQueryService.getBalanceInfo(userCode);

        String demoBalance = StringUtils.isNotEmpty(balanceInfo.getDemoBalance()) ? "PKR" + balanceInfo.getDemoBalance() : null;
        String realBalance = StringUtils.isNotEmpty(balanceInfo.getRealBalance()) ? "PKR" + balanceInfo.getRealBalance() : null;
        if (StringUtils.isNotEmpty(demoBalance) && StringUtils.isEmpty(realBalance)) {
            realBalance = "PKR0";
        }
        return new ApiResponse(new AccountBalanceResponse(demoBalance, realBalance));
    }

    /**
     * 获取银行名称
     *
     * @param queryBankNameRequest
     * @return
     */
    @VisitorAccessible
    @RequestMapping("/queryBankName")
    public ApiResponse<QueryBankNameResponse> queryBankName(@RequestBody QueryBankNameRequest queryBankNameRequest) {
        String bankName = withdrawService.getBankName(queryBankNameRequest.getIfsc());
        if (StringUtils.isEmpty(bankName)) {
            throw new ApplicationException("Please enter the correct IFSC");
        } else {
            return new ApiResponse<>(new QueryBankNameResponse(bankName));
        }
    }

    /**
     * 获取奖励金列表
     * @return
     */
    @RequestMapping("/grants/list")
    public ApiResponse<GrantsListResponse> queryGrantsList(@RequestBody AccountGrantsRequest accountGrantsRequest) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        AccountGrantsList userGrantsListByPage = accountQueryService.getUserGrantsListByPage(userCode, accountGrantsRequest.getPageNo());
        return new ApiResponse<>(new GrantsListResponse(userGrantsListByPage.isHasNextPage(), userGrantsListByPage.getAccountGrants()));
    }


}